package com.zbkj.dal.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zbkj.dal.entity.user.User;
import com.zbkj.dal.pojo.dto.UserFriendCountDTO;
import com.zbkj.dal.pojo.response.UserSpreadPeopleItemResponse;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 用户表 Mapper 接口
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
public interface UserDao extends BaseMapper<User> {

    List<User> findAdminList(Map<String, Object> map);

    List<User> findMerchantList(Map<String, Object> map);

    List<UserSpreadPeopleItemResponse> getSpreadPeopleList(Map<String, Object> map);

    @Select("select spread_uid as userId, count(*) as friendCount from eb_user where spread_uid in #{userIdList} and status = 1 group by spread_uid")
    List<UserFriendCountDTO> userFriendCount(@Param("userIdList") List<Integer> userIdList);

    @Transactional(rollbackFor = Exception.class)
    @Update("update eb_user u set u.now_money = #{amount}, u.update_time = current_timestamp where u.id = #{uid}")
    boolean updateAvailableAmount(@Param("uid") Integer uid, @Param("amount") BigDecimal amount);

    @Update("update eb_user u set u.spread_count = u.spread_count + 1, u.update_time = current_timestamp where u.id = #{uid}")
    boolean updateSpreadCount(@Param("uid") Integer uid);

    @Transactional(rollbackFor = Exception.class)
    @Update("update eb_user u set u.dues_amount = #{quota}, u.credit_amount = #{quota}, u.level = #{level}, u.update_time = current_timestamp where u.id = #{uid}")
    boolean updateUserQuota(@Param("uid") Integer uid, @Param("quota") BigDecimal quota, @Param("level") Integer level);
}
